﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class CalendarGroupDA : ICalendarGroupDA
    {
        private string CALENDAR_GROUP_ID = "@CALENDAR_GROUP_ID";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string END_TIME = "@END_TIME";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string SQL_CHECK_CALENDAR_GROUP_ID_UNIQUE = " SELECT COUNT(1) FROM @_@CALENDAR_GROUP WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID ";
        private string SQL_DELETE_CALENDAR_GROUP = " DELETE FROM @_@CALENDAR_GROUP WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID ";
        private string SQL_INSERT_CALENDAR_GROUP = " INSERT INTO @_@CALENDAR_GROUP ( WH_ID, CALENDAR_GROUP_ID, START_TIME, END_TIME, DESCR, IS_ACTIVE, UPDATED_DATE, UPDATED_BY, CREATED_DATE, CREATED_BY) VALUES ( @WH_ID, @CALENDAR_GROUP_ID, @START_TIME, @END_TIME, @DESCR, @IS_ACTIVE, GETDATE(), @UPDATED_BY, GETDATE(), @CREATED_BY )  ";
        private string SQL_SELECT_ALL_CALENDAR_GROUP = " SELECT  ROWID, WH_ID, CALENDAR_GROUP_ID, START_TIME, END_TIME, DESCR, TS, IS_ACTIVE, UPDATED_DATE, UPDATED_BY, CREATED_DATE, CREATED_BY  FROM  @_@CALENDAR_GROUP ";
        private string SQL_UPDATE_CALENDAR_GROUP = " UPDATE @_@CALENDAR_GROUP SET WH_ID = @WH_ID, START_TIME = @START_TIME, END_TIME = @END_TIME, DESCR = @DESCR, IS_ACTIVE = @IS_ACTIVE, UPDATED_DATE = GETDATE(), UPDATED_BY = @UPDATED_BY WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID  ";
        private string START_TIME = "@START_TIME";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckCalendarGroupIDUnique(string calendarGroupID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_CALENDAR_GROUP_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckCalendarGroupIDUnique(DataBase dataBase, DbTransaction tran, string calendarGroupID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_CALENDAR_GROUP_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteCalendarGroup(List<string> calendarGroupID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < calendarGroupID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CALENDAR_GROUP.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteCalendarGroup(DataBase dataBase, DbTransaction tran, List<string> calendarGroupID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < calendarGroupID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendarGroupID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_CALENDAR_GROUP.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllCalendarGroup(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllCalendarGroup(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public CalendarGroupInfo GetCalendarGroupByID(string calendargroupID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP.Replace("@_@", whLoginID) + " WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendargroupID) };
            CalendarGroupInfo calendarGroupInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    calendarGroupInfo = new CalendarGroupInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00003 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00003 = new DateTime()), reader["DESCR"].ToString(), (byte[]) reader["TS"], reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["CREATED_BY"].ToString());
                }
            }
            return calendarGroupInfo;
        }

        public CalendarGroupInfo GetCalendarGroupByID(DataBase dataBase, DbTransaction tran, string calendargroupID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_CALENDAR_GROUP.Replace("@_@", whLoginID) + " WHERE CALENDAR_GROUP_ID = @CALENDAR_GROUP_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@CALENDAR_GROUP_ID", calendargroupID) };
            CalendarGroupInfo calendarGroupInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                calendarGroupInfo = new CalendarGroupInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["CALENDAR_GROUP_ID"].ToString(), (reader["START_TIME"].ToString() != "") ? DateTime.Parse(reader["START_TIME"].ToString()) : (CS00003 = new DateTime()), (reader["END_TIME"].ToString() != "") ? DateTime.Parse(reader["END_TIME"].ToString()) : (CS00003 = new DateTime()), reader["DESCR"].ToString(), (byte[]) reader["TS"], reader["IS_ACTIVE"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["CREATED_BY"].ToString());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return calendarGroupInfo;
        }

        public DataSet GetCalendarGroupByQueryList(List<string> sqlWhere, CalendarGroupQueryEntity calendargroupQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CALENDAR_GROUP.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!calendargroupQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, calendargroupQuery.CurrentPage, calendargroupQuery.PageSize, calendargroupQuery.SortField, calendargroupQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetCalendarGroupByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, CalendarGroupQueryEntity calendargroupQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_CALENDAR_GROUP.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, calendargroupQuery.CurrentPage, calendargroupQuery.PageSize, calendargroupQuery.SortField, calendargroupQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertCalendarGroup(CalendarGroupInfo calendarGroupInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                calendarGroupInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroup_Parameters(calendarGroupInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CALENDAR_GROUP.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertCalendarGroup(DataBase dataBase, DbTransaction tran, CalendarGroupInfo calendarGroupInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                calendarGroupInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroup_Parameters(calendarGroupInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_CALENDAR_GROUP.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_CalendarGroup_Parameters(CalendarGroupInfo calendarGroupInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.CALENDAR_GROUP_ID, SqlDbType.VarChar, 20), new SqlParameter(this.START_TIME, SqlDbType.DateTime, 8), new SqlParameter(this.END_TIME, SqlDbType.DateTime, 8), new SqlParameter(this.DESCR, SqlDbType.VarChar, 100), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(calendarGroupInfo.WhID))
            {
                paramArray[0].Value = calendarGroupInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = calendarGroupInfo.CalendarGroupID;
            DateTime CS00003 = new DateTime();
            if (calendarGroupInfo.StartTime != CS00003)
            {
                paramArray[2].Value = calendarGroupInfo.StartTime;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            if (calendarGroupInfo.EndTime != new DateTime())
            {
                paramArray[3].Value = calendarGroupInfo.EndTime;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(calendarGroupInfo.Descr))
            {
                paramArray[4].Value = calendarGroupInfo.Descr;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = calendarGroupInfo.IsActive;
            paramArray[6].Value = calendarGroupInfo.UpdatedBy;
            paramArray[7].Value = calendarGroupInfo.CreatedBy;
            return paramArray;
        }

        public int UpdateCalendarGroup(CalendarGroupInfo calendarGroupInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                calendarGroupInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroup_Parameters(calendarGroupInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CALENDAR_GROUP.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateCalendarGroup(DataBase dataBase, DbTransaction tran, CalendarGroupInfo calendarGroupInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                calendarGroupInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_CalendarGroup_Parameters(calendarGroupInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_CALENDAR_GROUP.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

